package com.webank.webase.yidian;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.webank.webase.paillier.MyPaillierCipher;
import com.webank.webase.paillier.PaillierCipher;
import com.webank.webase.paillier.PaillierKeyPair;
import de.scravy.pair.Pair;
import org.apache.tomcat.util.codec.binary.Base64;
import org.springframework.web.bind.annotation.*;

import java.io.FileWriter;
import java.io.IOException;
import java.math.BigInteger;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.interfaces.RSAKey;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.KeySpec;
import java.util.Objects;
import java.util.Random;

@RestController
public class ProcessController {

    private TransactionService ts = new TransactionService();

    private static String pubKeyStr= "-----BEGIN PUBLIC KEY-----\n" +
            "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEApRp62pCMjQrK/6ZIM0B+\n" +
            "EbjjA8K5slr7aR4/8/FTOfXnGN5HT/hA6wmjmAOyL4SjNA5kyYAsHGRPIgI8CcxF\n" +
            "mE/sTRV+RHfGWBgTsuY/O3LKzpAypp5sVTx18Y06VvvkPcXlgKF1XNODZ6tbA7Wl\n" +
            "yzN7aav5gbBUXAvhEqmTIN0XTlOBnI+/33svc0aIefwO6hdz4QP4cyWJFexDCr1v\n" +
            "A+iRVY/VYnkNAyF2taI7uoz0nVGEbQ0vCxVEkPBHUQdLqSrkT949MZbHVaBkMSxb\n" +
            "F1wk2IB6DAYsWKf6gWxHlyhAXBuHssVv12J/r73MIP/b/Z0ZXB2cEvJC932f2PTx\n" +
            "GQIDAQAB\n" +
            "-----END PUBLIC KEY-----\n";

    private static String priKeyStr = "-----BEGIN PRIVATE KEY-----\n" +
            "MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQClGnrakIyNCsr/\n" +
            "pkgzQH4RuOMDwrmyWvtpHj/z8VM59ecY3kdP+EDrCaOYA7IvhKM0DmTJgCwcZE8i\n" +
            "AjwJzEWYT+xNFX5Ed8ZYGBOy5j87csrOkDKmnmxVPHXxjTpW++Q9xeWAoXVc04Nn\n" +
            "q1sDtaXLM3tpq/mBsFRcC+ESqZMg3RdOU4Gcj7/fey9zRoh5/A7qF3PhA/hzJYkV\n" +
            "7EMKvW8D6JFVj9VieQ0DIXa1oju6jPSdUYRtDS8LFUSQ8EdRB0upKuRP3j0xlsdV\n" +
            "oGQxLFsXXCTYgHoMBixYp/qBbEeXKEBcG4eyxW/XYn+vvcwg/9v9nRlcHZwS8kL3\n" +
            "fZ/Y9PEZAgMBAAECggEAa95rGdWLHrNIgdLgJSwjsex71X8p2A99ZuZunjCzUZEJ\n" +
            "lcPUD7mAoI9BpRX1Gvqrrl0hGE13hG09DlkrZLNjXOoK1yoNvyoKGpBQfnfUJEER\n" +
            "o/euQBINcXr5DAJbdW1JZ9qd4XabuTQwDFF2O2w4kBRos1e6wjK0SxcvzAOirrr3\n" +
            "r2TWcCDTpTCAmBcxUPN7MLl3M0iVqnWQ4PLBrs62srsTK3T7V7gHq0Pn1wbXcWC8\n" +
            "a+o02Ki6d7rIBznFLRDMV9+3hXvoDOSLno9E6CCCDWupKFXsFWQ70Xi86n9xXG+n\n" +
            "NcFJ5YOOSkWnqs7sxk2W9G5EvbUjLtQi0Fc9vFl3aQKBgQDUeMG7VZDw7AVzZwxk\n" +
            "p+Lo7jnWVsbGThoILftYnlr8d9NRXZSnE6AFWfSnbGfnMQA0tpYFpGFW+volgAch\n" +
            "gkjNppJ2jyRFbUymSW21R7C6zRyp0i5RcwOVeqetbRNDss6ZOD5tOz0x9uEepTJH\n" +
            "vSGe67pwCpy8ePK9Tk3ER9PWXwKBgQDG7XUjGSqTFEtn7z5BwvqkyveZhIxr0mYP\n" +
            "85oWgGxQvxVeu3yhA1DAwa295OZNK4zVouSsMZDWR0txg3pDN+AYubCNALcEYJJZ\n" +
            "kIb8yJEyUQeWXBkkbbjElTM21LswFoSnjNkr4mNzCpu+9AyaD9cIVjc37LnAF/CE\n" +
            "eWdys4k7hwKBgGm8Jnz5mgWrDxNdBtfYjJ1qYDIAQS9SkrXbXCe/tnvJjcoCDgYP\n" +
            "SCUhnRTsZ9Cvm/CfpIzuPtqwT2f2DJZqZZyh9mU12tEKEYj3Jbd/E74CcO85/37d\n" +
            "A72RqnwtudaI1zN8v+RQBKYmu+UqRJlaz65slP1I6rMIZZQ7sYuqSwt1AoGAbaPJ\n" +
            "EPw46/Gekf1ZqfMmJBe+Gf3lSzQejqIsBkTn7iUzSRA645+pLWRSNzxm9pePOtxZ\n" +
            "/tIeywtmeH0HsO6l89q8hSqktKPpZbXzwHVnGX1xmhzOurZCxQ6auJjNRvbSTGqj\n" +
            "870GJQzuxHNMUslyMiUasBcoVcicBtfG/YAYnhcCgYA7cTAmzvnIiXPR2/BYQSuf\n" +
            "YBL45Izgj5gV/rp2SiuItD3vGNEWJRHHwaUH2s5OqZMdLBvYmncMBoFyrmRCh+Ur\n" +
            "7JdrV+nfeKAqaoRSbOfuMjc/1e2IcmYvm1G+5K2rA5l10NIFwLMSxwRE5tVufBh3\n" +
            "UDUidIJ/jvWQbwtXz2aD2g==\n" +
            "-----END PRIVATE KEY-----\n";

    private static RSAPublicKey pubKey = (RSAPublicKey) PaillierKeyPair.pemToPublicKey(pubKeyStr);
    private static RSAPrivateKey priKey = (RSAPrivateKey) PaillierKeyPair.pemToPrivateKey(priKeyStr);

    {
        // 构造合约信息
        ts.setContractName("Accounting");
        ts.setContractAddress("0x2e5fe5c0ee99133f864bf7f3d14618716564ae2c");
        ts.setContractAbi("[{\"constant\":false,\"inputs\":[{\"name\":\"company_address\",\"type\":\"address\"},{\"name\":\"ids\",\"type\":\"string[]\"},{\"name\":\"company_id\",\"type\":\"string[]\"},{\"name\":\"is_unspent\",\"type\":\"string[]\"},{\"name\":\"spent_timestamp\",\"type\":\"string[]\"},{\"name\":\"spent_for_certificate_id\",\"type\":\"string[]\"},{\"name\":\"create_timestamp\",\"type\":\"string[]\"}],\"name\":\"insert_accounting_entry_step_3\",\"outputs\":[{\"name\":\"\",\"type\":\"int256\"}],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"company_address\",\"type\":\"address\"},{\"name\":\"ids\",\"type\":\"string[]\"},{\"name\":\"actual_certificate_id\",\"type\":\"string[]\"},{\"name\":\"abstraction\",\"type\":\"string[]\"},{\"name\":\"spent_accounting_entry_id\",\"type\":\"string[]\"},{\"name\":\"original_accounting_entry_id\",\"type\":\"string[]\"},{\"name\":\"original_document_hash\",\"type\":\"string[]\"}],\"name\":\"insert_accounting_entry_step_2\",\"outputs\":[{\"name\":\"\",\"type\":\"int256\"}],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"company_address\",\"type\":\"address\"},{\"name\":\"company_id\",\"type\":\"string\"},{\"name\":\"ids\",\"type\":\"string[]\"},{\"name\":\"is_unspent\",\"type\":\"string\"},{\"name\":\"spent_timestamp\",\"type\":\"string\"},{\"name\":\"spent_for_certificate_id\",\"type\":\"string\"}],\"name\":\"spend_utxo\",\"outputs\":[{\"name\":\"\",\"type\":\"int256\"}],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"company_address\",\"type\":\"address\"},{\"name\":\"ids\",\"type\":\"string[]\"},{\"name\":\"certificate_id\",\"type\":\"string[]\"},{\"name\":\"accounting_title_serial_number\",\"type\":\"string[]\"},{\"name\":\"accounting_title_name\",\"type\":\"string[]\"},{\"name\":\"accounting_entry_type\",\"type\":\"string[]\"},{\"name\":\"debit_amount\",\"type\":\"string[]\"},{\"name\":\"credit_amount\",\"type\":\"string[]\"}],\"name\":\"insert_accounting_entry_step_1\",\"outputs\":[{\"name\":\"\",\"type\":\"int256\"}],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"company_address\",\"type\":\"address\"},{\"name\":\"certificate\",\"type\":\"string[]\"}],\"name\":\"insert_certificate\",\"outputs\":[{\"name\":\"\",\"type\":\"int256\"}],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[{\"name\":\"cipher1\",\"type\":\"string\"},{\"name\":\"cipher2\",\"type\":\"string\"}],\"name\":\"add\",\"outputs\":[{\"name\":\"\",\"type\":\"string\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[],\"name\":\"create\",\"outputs\":[{\"name\":\"\",\"type\":\"int256\"}],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"inputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"constructor\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":false,\"name\":\"owner\",\"type\":\"address\"},{\"indexed\":false,\"name\":\"table_name\",\"type\":\"string\"}],\"name\":\"createEvent\",\"type\":\"event\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":false,\"name\":\"company_address\",\"type\":\"address\"},{\"indexed\":false,\"name\":\"certificate\",\"type\":\"string[]\"}],\"name\":\"insertCertificateEvent\",\"type\":\"event\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":false,\"name\":\"company_address\",\"type\":\"address\"},{\"indexed\":false,\"name\":\"id\",\"type\":\"string\"},{\"indexed\":false,\"name\":\"certificate_id\",\"type\":\"string\"},{\"indexed\":false,\"name\":\"accounting_title_serial_number\",\"type\":\"string\"},{\"indexed\":false,\"name\":\"accounting_title_name\",\"type\":\"string\"},{\"indexed\":false,\"name\":\"accounting_entry_type\",\"type\":\"string\"},{\"indexed\":false,\"name\":\"debit_amount\",\"type\":\"string\"},{\"indexed\":false,\"name\":\"credit_amount\",\"type\":\"string\"}],\"name\":\"insertAccountingEntryStep1Event\",\"type\":\"event\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":false,\"name\":\"company_address\",\"type\":\"address\"},{\"indexed\":false,\"name\":\"id\",\"type\":\"string\"},{\"indexed\":false,\"name\":\"actual_certificate_id\",\"type\":\"string\"},{\"indexed\":false,\"name\":\"abstraction\",\"type\":\"string\"},{\"indexed\":false,\"name\":\"spent_accounting_entry_id\",\"type\":\"string\"},{\"indexed\":false,\"name\":\"original_accounting_entry_id\",\"type\":\"string\"},{\"indexed\":false,\"name\":\"original_document_hash\",\"type\":\"string\"}],\"name\":\"insertAccountingEntryStep2Event\",\"type\":\"event\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":false,\"name\":\"company_address\",\"type\":\"address\"},{\"indexed\":false,\"name\":\"id\",\"type\":\"string\"},{\"indexed\":false,\"name\":\"company_id\",\"type\":\"string\"},{\"indexed\":false,\"name\":\"is_unspent\",\"type\":\"string\"},{\"indexed\":false,\"name\":\"spent_timestamp\",\"type\":\"string\"},{\"indexed\":false,\"name\":\"spent_for_certificate_id\",\"type\":\"string\"},{\"indexed\":false,\"name\":\"create_timestamp\",\"type\":\"string\"}],\"name\":\"insertAccountingEntryStep3Event\",\"type\":\"event\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":false,\"name\":\"company_address\",\"type\":\"address\"},{\"indexed\":false,\"name\":\"company_id\",\"type\":\"string\"},{\"indexed\":false,\"name\":\"id\",\"type\":\"string\"},{\"indexed\":false,\"name\":\"is_unspent\",\"type\":\"string\"},{\"indexed\":false,\"name\":\"spent_timestamp\",\"type\":\"string\"},{\"indexed\":false,\"name\":\"spent_for_certificate_id\",\"type\":\"string\"}],\"name\":\"spendUtxoEvent\",\"type\":\"event\"}]");
    }

    /**
     *
     * @param jsonParam
     * 接收数据格式：
     * {
     *     "data": {
     *         "company_address": "1",
     *         "company_id": "1",
     *         "id": ["1"],
     *         "is_unspent": "0",
     *         "spent_timestamp": "1600655081",
     *         "spent_for_certificate_id": "53"
     *     }
     * }
     * @return
     * 返回数据格式：
     * {
     *     "Msg": "SUCCESS",
     *     "Data": {
     *         "blockHash": "0xd819063deb48d75cd574956652c87a56a3b81d8e68e67674b39dc2aea807f74a",
     *         "logsBloom": "0x00000000040000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000",
     *         "contractAddress": "0x0000000000000000000000000000000000000000",
     *         "transactionIndex": 0,
     *         "transactionHash": "0x0da8434589e2463abe6bf34679fe6a8d5855690ee19e57c856a36964b4d4254c",
     *         "output": "0x0000000000000000000000000000000000000000000000000000000000000000",
     *         "input": "0x9f572f66000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000c00000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000018000000000000000000000000000000000000000000000000000000000000001c0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000013100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000001310000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000013000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a313630303635353038310000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000023533000000000000000000000000000000000000000000000000000000000000",
     *         "gasUsed": 74864,
     *         "blockNumber": 61,
     *         "root": "0x824b30e6ab65798d9f76154a87f810ffee9c3d18a52122ace31ed714552fe61d",
     *         "from": "0xab35fbc6ee5c111adae055e4d0deedfc51bd5f24",
     *         "to": "0x1d17af97a9d2590082027721cbe32af7dd977c32",
     *         "logs": [
     *             {
     *                 "address": "0x1d17af97a9d2590082027721cbe32af7dd977c32",
     *                 "data": "0x000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000140000000000000000000000000000000000000000000000000000000000000018000000000000000000000000000000000000000000000000000000000000001c0000000000000000000000000000000000000000000000000000000000000000131000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001310000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000013000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a313630303635353038310000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000023533000000000000000000000000000000000000000000000000000000000000",
     *                 "topics": [
     *                     "0x14b6c1b8b587593aea772af07b3c458a7e851fb899ca26386d051d846c53c0cc"
     *                 ]
     *             }
     *         ],
     *         "status": "0x0"
     *     },
     *     "ErrorCode": 0
     * }
     */
    @RequestMapping(value = "/update_spend_utxo", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public JSONObject update_spend_utxo(@RequestBody JSONObject jsonParam) {
        // 直接将json信息打印出来
        System.out.println("Interface update_spend_utxo:" + jsonParam.toJSONString());

        JSONObject data = jsonParam.getJSONObject("data");
//        System.out.println(data);

        SpendUtxoEntity sue = new SpendUtxoEntity();
        sue.setCompany_address(data.getString("company_address"));
        sue.setCompany_id(data.getString("company_id"));
        sue.setId(jsonArrayToStringArray(data.getJSONArray("id")));
        sue.setIs_unspent(data.getString("is_unspent"));
        sue.setSpent_timestamp(data.getString("spent_timestamp"));
        sue.setSpent_for_certificate_id(data.getString("spent_for_certificate_id"));

        ts.setFuncName("spend_utxo");
        String para = sue.submit();
        ts.setFuncParam(para);
        Pair<Integer, String> obj = ts.sendTransaction();

        JSONObject result = new JSONObject();
        if(obj.getFirst() != 0){
            result.put("Msg", "ERROR in sendTransaction spend_utxo.");
            result.put("ErrorCode", 1);
        }else {
            System.out.println("spend_utxo results:" + obj);
            if (obj.getSecond().equals("null")) {
                result.put("Data", obj.getSecond());
            } else {
                result.put("Data", JSON.parseObject(obj.getSecond()));
            }
        }

        result.put("Msg", "SUCCESS");
        result.put("ErrorCode", 0);
        return result;

    }

    public String[] jsonArrayToStringArray(JSONArray data) {
        int num = data.size();

        String[] results = new String[num];

        for (int i = 0; i < num; i++) {
            results[i] = data.getString(i);
        }

        return results;
    }

    /**
     *
     * @param jsonParam
     * 接收数据格式：
     * {
     *     "data": {
     *         "company_address": "1",
     *         "accounting": [
     *             {
     *                 "id": "83",
     *                 "certificate_id": "0",
     *                 "actual_certificate_id": "54",
     *                 "abstraction": "找零生成",
     *                 "accounting_title_serial_number": "1002",
     *                 "accounting_title_name": "银行存款",
     *                 "type": "1",
     *                 "spent_accounting_entry_id": "82",
     *                 "original_accounting_entry_id": "82",
     *                 "debit_amount": "800",
     *                 "credit_amount": "0",
     *                 "original_document_hash": "",
     *                 "company_id": "1",
     *                 "is_unspent": "",
     *                 "spent_timestamp": "",
     *                 "spent_for_certificate_id": "",
     *                 "create_timestamp": "1600656041"
     *             },
     *             {
     *                 "id": "84",
     *                 "certificate_id": "54",
     *                 "actual_certificate_id": "54",
     *                 "abstraction": null,
     *                 "accounting_title_serial_number": "1002",
     *                 "accounting_title_name": "银行存款",
     *                 "type": "2",
     *                 "spent_accounting_entry_id": "0",
     *                 "original_accounting_entry_id": "0",
     *                 "debit_amount": "0",
     *                 "credit_amount": "200",
     *                 "original_document_hash": "",
     *                 "company_id": "1",
     *                 "is_unspent": "",
     *                 "spent_timestamp": "",
     *                 "spent_for_certificate_id": "",
     *                 "create_timestamp": "1600656041"
     *             },
     *             {
     *                 "id": "85",
     *                 "certificate_id": "54",
     *                 "actual_certificate_id": "54",
     *                 "abstraction": "222",
     *                 "accounting_title_serial_number": "1406",
     *                 "accounting_title_name": "库存商品",
     *                 "type": "1",
     *                 "spent_accounting_entry_id": "0",
     *                 "original_accounting_entry_id": "0",
     *                 "debit_amount": "100",
     *                 "credit_amount": "0",
     *                 "original_document_hash": "",
     *                 "company_id": "1",
     *                 "is_unspent": "",
     *                 "spent_timestamp": "",
     *                 "spent_for_certificate_id": "",
     *                 "create_timestamp": "1600656041"
     *             },
     *             {
     *                 "id": "86",
     *                 "certificate_id": "54",
     *                 "actual_certificate_id": "54",
     *                 "abstraction": "333",
     *                 "accounting_title_serial_number": "1403",
     *                 "accounting_title_name": "原材料",
     *                 "type": "1",
     *                 "spent_accounting_entry_id": "0",
     *                 "original_accounting_entry_id": "0",
     *                 "debit_amount": "100",
     *                 "credit_amount": "0",
     *                 "original_document_hash": "",
     *                 "company_id": "1",
     *                 "is_unspent": "",
     *                 "spent_timestamp": "",
     *                 "spent_for_certificate_id": "",
     *                 "create_timestamp": "1600656041"
     *             }
     *         ]
     *     }
     * }
     * @return
     * 返回数据格式：
     * {
     *     "Msg": "SUCCESS",
     *     "Data": {
     *         "res1": {
     *             "blockHash": "0x28144aaa8d075dbfa2c7ef2e6d98ec7d067df65ff5a25af70710efd68d67beb7",
     *             "logsBloom": "0x00000000040000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000002000000000000000000000000000000000000800008000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
     *             "contractAddress": "0x0000000000000000000000000000000000000000",
     *             "transactionIndex": 0,
     *             "transactionHash": "0x47325f201e6c4ea72f5544d8014dd3b2f7225162d274e4d896d819d124f61c23",
     *             "output": "0x0000000000000000000000000000000000000000000000000000000000000004",
     *             "input": "0xc14fa9160000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002a0000000000000000000000000000000000000000000000000000000000000044000000000000000000000000000000000000000000000000000000000000005e0000000000000000000000000000000000000000000000000000000000000078000000000000000000000000000000000000000000000000000000000000009200000000000000000000000000000000000000000000000000000000000000ac00000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000140000000000000000000000000000000000000000000000000000000000000000238330000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002383400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000023835000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000238360000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000140000000000000000000000000000000000000000000000000000000000000000130000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002353400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000023534000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000235340000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000140000000000000000000000000000000000000000000000000000000000000000431303032000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004313030320000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000043134303600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000431343033000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000140000000000000000000000000000000000000000000000000000000000000000ce993b6e8a18ce5ad98e6acbe0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000ce993b6e8a18ce5ad98e6acbe0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000ce5ba93e5ad98e59586e5938100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009e58e9fe69d90e6969900000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000140000000000000000000000000000000000000000000000000000000000000000131000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001320000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000013100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000131000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000140000000000000000000000000000000000000000000000000000000000000000338303000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000033130300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000331303000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000c00000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000014000000000000000000000000000000000000000000000000000000000000000013000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000332303000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000013000000000000000000000000000000000000000000000000000000000000000",
     *             "gasUsed": 250677,
     *             "blockNumber": 49,
     *             "root": "0x2ed2ef3c11412e8a27f87f023b92a13b4493f9963936ff3140980a5bd9d27854",
     *             "from": "0xab35fbc6ee5c111adae055e4d0deedfc51bd5f24",
     *             "to": "0x1d17af97a9d2590082027721cbe32af7dd977c32",
     *             "logs": [
     *                 {
     *                     "address": "0x1d17af97a9d2590082027721cbe32af7dd977c32",
     *                     "data": "0x000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000140000000000000000000000000000000000000000000000000000000000000018000000000000000000000000000000000000000000000000000000000000001c0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000002400000000000000000000000000000000000000000000000000000000000000280000000000000000000000000000000000000000000000000000000000000000238330000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000043130303200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000ce993b6e8a18ce5ad98e6acbe0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000131000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003383030000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000013000000000000000000000000000000000000000000000000000000000000000",
     *                     "topics": [
     *                         "0x9e3b011ca9d334fa3e8d31f31d38c91f59d31d8390392e05c688fb2d3a7f4b78"
     *                     ]
     *                 },
     *                 {
     *                     "address": "0x1d17af97a9d2590082027721cbe32af7dd977c32",
     *                     "data": "0x000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000140000000000000000000000000000000000000000000000000000000000000018000000000000000000000000000000000000000000000000000000000000001c0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000002400000000000000000000000000000000000000000000000000000000000000280000000000000000000000000000000000000000000000000000000000000000238340000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002353400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000043130303200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000ce993b6e8a18ce5ad98e6acbe0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000132000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000033230300000000000000000000000000000000000000000000000000000000000",
     *                     "topics": [
     *                         "0x9e3b011ca9d334fa3e8d31f31d38c91f59d31d8390392e05c688fb2d3a7f4b78"
     *                     ]
     *                 },
     *                 {
     *                     "address": "0x1d17af97a9d2590082027721cbe32af7dd977c32",
     *                     "data": "0x000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000140000000000000000000000000000000000000000000000000000000000000018000000000000000000000000000000000000000000000000000000000000001c0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000002400000000000000000000000000000000000000000000000000000000000000280000000000000000000000000000000000000000000000000000000000000000238350000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002353400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000043134303600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000ce5ba93e5ad98e59586e593810000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000131000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003313030000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000013000000000000000000000000000000000000000000000000000000000000000",
     *                     "topics": [
     *                         "0x9e3b011ca9d334fa3e8d31f31d38c91f59d31d8390392e05c688fb2d3a7f4b78"
     *                     ]
     *                 },
     *                 {
     *                     "address": "0x1d17af97a9d2590082027721cbe32af7dd977c32",
     *                     "data": "0x000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000140000000000000000000000000000000000000000000000000000000000000018000000000000000000000000000000000000000000000000000000000000001c00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000024000000000000000000000000000000000000000000000000000000000000002800000000000000000000000000000000000000000000000000000000000000002383600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000023534000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000431343033000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009e58e9fe69d90e696990000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000131000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003313030000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000013000000000000000000000000000000000000000000000000000000000000000",
     *                     "topics": [
     *                         "0x9e3b011ca9d334fa3e8d31f31d38c91f59d31d8390392e05c688fb2d3a7f4b78"
     *                     ]
     *                 }
     *             ],
     *             "status": "0x0"
     *         },
     *         "res3": {
     *             "blockHash": "0x9bf65dca6a4d323c76b9719805ee4b1f28ddfd873d1b03c6a9ceaa0b55f5a0c7",
     *             "logsBloom": "0x00000000040000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000080000000000000000000",
     *             "contractAddress": "0x0000000000000000000000000000000000000000",
     *             "transactionIndex": 0,
     *             "transactionHash": "0x7fcf9e53eeff2f217edc5e2a6aae81a428fdb2b21d41568e478fb9293f55ff7a",
     *             "output": "0x000000000000000000000000000000000000000000000000000000000000000c",
     *             "input": "0x62de971e000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000e0000000000000000000000000000000000000000000000000000000000000028000000000000000000000000000000000000000000000000000000000000004200000000000000000000000000000000000000000000000000000000000000540000000000000000000000000000000000000000000000000000000000000066000000000000000000000000000000000000000000000000000000000000007800000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000140000000000000000000000000000000000000000000000000000000000000000238330000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002383400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000023835000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000238360000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000140000000000000000000000000000000000000000000000000000000000000000131000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001310000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000013100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000131000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000000e000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000000e000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000000e000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000140000000000000000000000000000000000000000000000000000000000000000a3136303036353630343100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a3136303036353630343100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a3136303036353630343100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a3136303036353630343100000000000000000000000000000000000000000000",
     *             "gasUsed": 309045,
     *             "blockNumber": 51,
     *             "root": "0xa28ce0c3f28db1f12c868fa1cacac693edd0d53503f25962135fb777fb2887e0",
     *             "from": "0xab35fbc6ee5c111adae055e4d0deedfc51bd5f24",
     *             "to": "0x1d17af97a9d2590082027721cbe32af7dd977c32",
     *             "logs": [
     *                 {
     *                     "address": "0x1d17af97a9d2590082027721cbe32af7dd977c32",
     *                     "data": "0x000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000e000000000000000000000000000000000000000000000000000000000000001200000000000000000000000000000000000000000000000000000000000000160000000000000000000000000000000000000000000000000000000000000018000000000000000000000000000000000000000000000000000000000000001a000000000000000000000000000000000000000000000000000000000000001c00000000000000000000000000000000000000000000000000000000000000002383300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000013100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a3136303036353630343100000000000000000000000000000000000000000000",
     *                     "topics": [
     *                         "0xd0bd45ea54c14ff7ae0a902333c4a545ca7b9000c127b43f058282057e80b5ae"
     *                     ]
     *                 },
     *                 {
     *                     "address": "0x1d17af97a9d2590082027721cbe32af7dd977c32",
     *                     "data": "0x000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000e000000000000000000000000000000000000000000000000000000000000001200000000000000000000000000000000000000000000000000000000000000160000000000000000000000000000000000000000000000000000000000000018000000000000000000000000000000000000000000000000000000000000001a000000000000000000000000000000000000000000000000000000000000001c00000000000000000000000000000000000000000000000000000000000000002383400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000013100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a3136303036353630343100000000000000000000000000000000000000000000",
     *                     "topics": [
     *                         "0xd0bd45ea54c14ff7ae0a902333c4a545ca7b9000c127b43f058282057e80b5ae"
     *                     ]
     *                 },
     *                 {
     *                     "address": "0x1d17af97a9d2590082027721cbe32af7dd977c32",
     *                     "data": "0x000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000e000000000000000000000000000000000000000000000000000000000000001200000000000000000000000000000000000000000000000000000000000000160000000000000000000000000000000000000000000000000000000000000018000000000000000000000000000000000000000000000000000000000000001a000000000000000000000000000000000000000000000000000000000000001c00000000000000000000000000000000000000000000000000000000000000002383500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000013100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a3136303036353630343100000000000000000000000000000000000000000000",
     *                     "topics": [
     *                         "0xd0bd45ea54c14ff7ae0a902333c4a545ca7b9000c127b43f058282057e80b5ae"
     *                     ]
     *                 },
     *                 {
     *                     "address": "0x1d17af97a9d2590082027721cbe32af7dd977c32",
     *                     "data": "0x000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000e000000000000000000000000000000000000000000000000000000000000001200000000000000000000000000000000000000000000000000000000000000160000000000000000000000000000000000000000000000000000000000000018000000000000000000000000000000000000000000000000000000000000001a000000000000000000000000000000000000000000000000000000000000001c00000000000000000000000000000000000000000000000000000000000000002383600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000013100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a3136303036353630343100000000000000000000000000000000000000000000",
     *                     "topics": [
     *                         "0xd0bd45ea54c14ff7ae0a902333c4a545ca7b9000c127b43f058282057e80b5ae"
     *                     ]
     *                 }
     *             ],
     *             "status": "0x0"
     *         },
     *         "res2": {
     *             "blockHash": "0x69cc28aa8c8450a7dfd6058f535a439890a14006102f1bb991102b3a15c05c57",
     *             "logsBloom": "0x00000000040000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
     *             "contractAddress": "0x0000000000000000000000000000000000000000",
     *             "transactionIndex": 0,
     *             "transactionHash": "0x5037b55f21f77e557fb1be9c9d57ba7a62d672ee659a2376e14b121ba71c30a3",
     *             "output": "0x000000000000000000000000000000000000000000000000000000000000000c",
     *             "input": "0x69a96fb7000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000e00000000000000000000000000000000000000000000000000000000000000280000000000000000000000000000000000000000000000000000000000000042000000000000000000000000000000000000000000000000000000000000005a0000000000000000000000000000000000000000000000000000000000000074000000000000000000000000000000000000000000000000000000000000008e00000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000140000000000000000000000000000000000000000000000000000000000000000238330000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002383400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000023835000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000238360000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000140000000000000000000000000000000000000000000000000000000000000000235340000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002353400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000023534000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000235340000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000000e00000000000000000000000000000000000000000000000000000000000000120000000000000000000000000000000000000000000000000000000000000000ce689bee99bb6e7949fe688900000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000033232320000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000333333300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000140000000000000000000000000000000000000000000000000000000000000000238320000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000013000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000130000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000140000000000000000000000000000000000000000000000000000000000000000238320000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000013000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000130000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000000e00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
     *             "gasUsed": 313172,
     *             "blockNumber": 50,
     *             "root": "0x0189ded06cc576d4188847fc5abaca27621034f7d7d52163b620d062fd8b39c3",
     *             "from": "0xab35fbc6ee5c111adae055e4d0deedfc51bd5f24",
     *             "to": "0x1d17af97a9d2590082027721cbe32af7dd977c32",
     *             "logs": [
     *                 {
     *                     "address": "0x1d17af97a9d2590082027721cbe32af7dd977c32",
     *                     "data": "0x000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000e00000000000000000000000000000000000000000000000000000000000000120000000000000000000000000000000000000000000000000000000000000016000000000000000000000000000000000000000000000000000000000000001a000000000000000000000000000000000000000000000000000000000000001e000000000000000000000000000000000000000000000000000000000000002200000000000000000000000000000000000000000000000000000000000000002383300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000023534000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000ce689bee99bb6e7949fe68890000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000023832000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000238320000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
     *                     "topics": [
     *                         "0xdb778182eec87137355453dd1d5b30ba82b42c770afbb0fba659185c487ab6dd"
     *                     ]
     *                 },
     *                 {
     *                     "address": "0x1d17af97a9d2590082027721cbe32af7dd977c32",
     *                     "data": "0x000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000e000000000000000000000000000000000000000000000000000000000000001200000000000000000000000000000000000000000000000000000000000000160000000000000000000000000000000000000000000000000000000000000018000000000000000000000000000000000000000000000000000000000000001c000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000002383400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000023534000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000013000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000130000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
     *                     "topics": [
     *                         "0xdb778182eec87137355453dd1d5b30ba82b42c770afbb0fba659185c487ab6dd"
     *                     ]
     *                 },
     *                 {
     *                     "address": "0x1d17af97a9d2590082027721cbe32af7dd977c32",
     *                     "data": "0x000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000e00000000000000000000000000000000000000000000000000000000000000120000000000000000000000000000000000000000000000000000000000000016000000000000000000000000000000000000000000000000000000000000001a000000000000000000000000000000000000000000000000000000000000001e0000000000000000000000000000000000000000000000000000000000000022000000000000000000000000000000000000000000000000000000000000000023835000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000235340000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003323232000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000013000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000130000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
     *                     "topics": [
     *                         "0xdb778182eec87137355453dd1d5b30ba82b42c770afbb0fba659185c487ab6dd"
     *                     ]
     *                 },
     *                 {
     *                     "address": "0x1d17af97a9d2590082027721cbe32af7dd977c32",
     *                     "data": "0x000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000e00000000000000000000000000000000000000000000000000000000000000120000000000000000000000000000000000000000000000000000000000000016000000000000000000000000000000000000000000000000000000000000001a000000000000000000000000000000000000000000000000000000000000001e0000000000000000000000000000000000000000000000000000000000000022000000000000000000000000000000000000000000000000000000000000000023836000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000235340000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003333333000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000013000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000130000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
     *                     "topics": [
     *                         "0xdb778182eec87137355453dd1d5b30ba82b42c770afbb0fba659185c487ab6dd"
     *                     ]
     *                 }
     *             ],
     *             "status": "0x0"
     *         }
     *     },
     *     "ErrorCode": 0
     * }
     */
    @RequestMapping(value = "/insert_accounting_entry", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public JSONObject insert_accounting_entry(@RequestBody JSONObject jsonParam) {
        // 直接将json信息打印出来
        System.out.println("Interface insert_accounting_entry:"+jsonParam.toJSONString());

        JSONObject data = jsonParam.getJSONObject("data");
//        System.out.println(data);

        AccountingEntryEntity ae = new AccountingEntryEntity();
        ae.setCompany_address(data.getString("company_address"));

        // 组装数据
        JSONArray accounting = data.getJSONArray("accounting");
        ae.setId(getStringArrayFromJson("id", accounting));
        ae.setCertificate_id(getStringArrayFromJson("certificate_id", accounting));
        ae.setActual_certificate_id(getStringArrayFromJson("actual_certificate_id", accounting));
        ae.setAbstraction(getStringArrayFromJson("abstraction", accounting));
        ae.setAccounting_title_serial_number(getStringArrayFromJson("accounting_title_serial_number", accounting));
        ae.setAccounting_title_name(getStringArrayFromJson("accounting_title_name", accounting));
        ae.setAccounting_entry_type(getStringArrayFromJson("type", accounting));
        ae.setSpent_accounting_entry_id(getStringArrayFromJson("spent_accounting_entry_id", accounting));
        ae.setOriginal_accounting_entry_id(getStringArrayFromJson("original_accounting_entry_id", accounting));
        ae.setDebit_amount(getStringArrayFromJson("debit_amount", accounting));
        ae.setCredit_amount(getStringArrayFromJson("credit_amount", accounting));
        ae.setOriginal_document_hash(getStringArrayFromJson("original_document_hash", accounting));
        ae.setCompany_id(getStringArrayFromJson("company_id", accounting));
        ae.setIs_unspent(getStringArrayFromJson("is_unspent", accounting));
        ae.setSpent_timestamp(getStringArrayFromJson("spent_timestamp", accounting));
        ae.setSpent_for_certificate_id(getStringArrayFromJson("spent_for_certificate_id", accounting));
        ae.setCreate_timestamp(getStringArrayFromJson("create_timestamp", accounting));

        JSONObject result = new JSONObject();
        JSONObject datatmp = new JSONObject();

        // 做同态加密

        BigInteger n = ((RSAPublicKey)pubKey).getModulus();
        BigInteger random;
        do {
            random = new BigInteger(n.bitLength(), new Random());
        } while (random.signum() != 1);

        try {
            String[] debit = ae.getDebit_amount();
            String[] debit_new = new String[debit.length];
            for (int i = 0; i < debit.length; i++) {
                BigInteger bin = BigInteger.valueOf(Long.parseLong(debit[i]));
                String encrypt_str = MyPaillierCipher.encrypt(bin, pubKey, random);
                debit_new[i] = encrypt_str;
            }
            ae.setDebit_amount(debit_new);

            String[] credit = ae.getCredit_amount();
            String[] credit_new = new String[credit.length];
            for (int i = 0; i < credit.length; i++) {
                BigInteger bin = BigInteger.valueOf(Long.parseLong(credit[i]));
                String encrypt_str = MyPaillierCipher.encrypt(bin, pubKey, random);
                credit_new[i] = encrypt_str;
            }
            ae.setCredit_amount(credit_new);
        }catch (Exception e){
            result.put("Msg", "ERROR in Homomorphic Encryption: " + e.toString());
            result.put("ErrorCode", 4);
            return result;
        }

        // 请求区块链
        ts.setFuncName("insert_accounting_entry_step_1");
        String para1 = ae.step_first();
        ts.setFuncParam(para1);
        Pair<Integer, String> obj1 = ts.sendTransaction();

        if(obj1.getFirst() != 0){
            result.put("Msg", "ERROR in sendTransaction insert_accounting_entry_step_1.");
            result.put("ErrorCode", 1);
            return result;
        }else {
            System.out.println("insert_accounting_entry_step_1 results:" + obj1);
            if (obj1.getSecond().equals("null")) {
                datatmp.put("res1", obj1.getSecond());
            } else {
                datatmp.put("res1", JSON.parseObject(obj1.getSecond()));
            }
        }

        ts.setFuncName("insert_accounting_entry_step_2");
        String para2 = ae.step_second();
        ts.setFuncParam(para2);
        Pair<Integer, String> obj2 = ts.sendTransaction();

        if(obj2.getFirst() != 0){
            result.put("Data", datatmp);
            result.put("Msg", "ERROR in sendTransaction insert_accounting_entry_step_2.");
            result.put("ErrorCode", 2);
            return result;
        }else {
            System.out.println("insert_accounting_entry_step_2 results:" + obj2);
            if (obj1.getSecond().equals("null")) {
                datatmp.put("res2", obj2.getSecond());
            } else {
                datatmp.put("res2", JSON.parseObject(obj2.getSecond()));
            }
        }

        ts.setFuncName("insert_accounting_entry_step_3");
        String para3 = ae.step_third();
        ts.setFuncParam(para3);
        Pair<Integer, String> obj3 = ts.sendTransaction();

        if(obj3.getFirst() != 0){
            result.put("Data", datatmp);
            result.put("Msg", "ERROR in sendTransaction insert_accounting_entry_step_3.");
            result.put("ErrorCode", 3);
            return result;
        }else {
            System.out.println("insert_accounting_entry_step_3 results:" + obj3);
            if (obj1.getSecond().equals("null")) {
                datatmp.put("res3", obj3.getSecond());
            } else {
                datatmp.put("res3", JSON.parseObject(obj3.getSecond()));
            }
        }

        result.put("Msg", "SUCCESS");
        result.put("Data", datatmp);
        result.put("ErrorCode", 0);

        JSONObject dubug = new JSONObject();
        dubug.put("res1", para1);
        dubug.put("res2", para2);
        dubug.put("res3", para3);

        result.put("dubug", dubug);

        return result;
    }

    public String[] getStringArrayFromJson(String str, JSONArray data) {
        int num = data.size();

        String[] result = new String[num];

        for (int i = 0; i < num; i++) {
            JSONObject jobj = data.getJSONObject(i);
            result[i] = jobj.getString(str);
            if (result[i] == null) {
                result[i] = "";
            }
        }

        return result;
    }

    /**
     *
     * @param jsonParam
     * 接收数据格式：
     * {
     *     "data": {
     *         "company_address": "1",
     *         "certificate": {
     *             "id": "1",
     *             "certificate_number": "1",
     *             "year": "2020",
     *             "month": "9",
     *             "day": "28",
     *             "total_debit_amount": "1000",
     *             "total_credit_amount": "1000",
     *             "tabulator": "施建锋",
     *             "company_id": "99",
     *             "create_timestamp": "1601280472"
     *         }
     *     }
     * }
     * @return
     * 返回数据格式：
     * （1）请求成功
     * {
     *     "Msg": "SUCCESS",
     *     "Data": {
     *         "blockHash": "0x09b5e108f6c5694d74993292dbb886ee54907226f8a10ac4d621c40cdd22d9da",
     *         "logsBloom": "0x00000000040000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000020000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000040000000000",
     *         "contractAddress": "0x0000000000000000000000000000000000000000",
     *         "transactionIndex": 0,
     *         "transactionHash": "0x9e3a1abda1810a4e727e8d34cbaafd1ed72c3266585fb727bdc6775a60c4f8d8",
     *         "output": "0x0000000000000000000000000000000000000000000000000000000000000001",
     *         "input": "0xd08f852000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000000000140000000000000000000000000000000000000000000000000000000000000018000000000000000000000000000000000000000000000000000000000000001c000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000240000000000000000000000000000000000000000000000000000000000000028000000000000000000000000000000000000000000000000000000000000002c0000000000000000000000000000000000000000000000000000000000000030000000000000000000000000000000000000000000000000000000000000003400000000000000000000000000000000000000000000000000000000000000380000000000000000000000000000000000000000000000000000000000000000131000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001310000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000043230323000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000139000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002323800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000043130303000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000431303030000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009e696bde5bbbae9948b000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000023939000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a3136303132383034373200000000000000000000000000000000000000000000",
     *         "gasUsed": 109022,
     *         "blockNumber": 42,
     *         "root": "0x4555e0ce67ca3fe7cffaab4f59b70e134909b6e9a0675268b08ebae85bbd86dd",
     *         "from": "0xab35fbc6ee5c111adae055e4d0deedfc51bd5f24",
     *         "to": "0x1d17af97a9d2590082027721cbe32af7dd977c32",
     *         "logs": [
     *             {
     *                 "address": "0x1d17af97a9d2590082027721cbe32af7dd977c32",
     *                 "data": "0x00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000000000140000000000000000000000000000000000000000000000000000000000000018000000000000000000000000000000000000000000000000000000000000001c000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000240000000000000000000000000000000000000000000000000000000000000028000000000000000000000000000000000000000000000000000000000000002c0000000000000000000000000000000000000000000000000000000000000030000000000000000000000000000000000000000000000000000000000000003400000000000000000000000000000000000000000000000000000000000000380000000000000000000000000000000000000000000000000000000000000000131000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001310000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000043230323000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000139000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002323800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000043130303000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000431303030000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009e696bde5bbbae9948b000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000023939000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a3136303132383034373200000000000000000000000000000000000000000000",
     *                 "topics": [
     *                     "0xde2c21391e56b3f234183c33e2bf0bf3f3930644381b7969ca5d9bf860912a9f"
     *                 ]
     *             }
     *         ],
     *         "status": "0x0"
     *     },
     *     "ErrorCode": 0
     * }
     * （2）请求失败
     * {
     *     "Msg": "ERROR in sendTransaction.",
     *     "ErrorCode": 1
     * }
     */
    @RequestMapping(value = "/insert_certificate", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public JSONObject insert_certificate(@RequestBody JSONObject jsonParam) {
        // 直接将json信息打印出来
        System.out.println("Interface insert_certificate:" + jsonParam.toJSONString());

        JSONObject data = jsonParam.getJSONObject("data");
//        System.out.println(data);

        CertificateEntity ce = new CertificateEntity();
        ce.setCompany_address(data.getString("company_address"));

        JSONObject certificate = data.getJSONObject("certificate");

        ce.setId(certificate.getString("id"));
        ce.setCertificate_number(certificate.getString("certificate_number"));
        ce.setYear(certificate.getString("year"));
        ce.setMonth(certificate.getString("month"));
        ce.setDay(certificate.getString("day"));
        ce.setTotal_debit_amount(certificate.getString("total_debit_amount"));
        ce.setTotal_credit_amount(certificate.getString("total_credit_amount"));
        ce.setTabulator(certificate.getString("tabulator"));
        ce.setCompany_id(certificate.getString("company_id"));
        ce.setCreate_timestamp(certificate.getString("create_timestamp"));
        // 构造输入参数对象完毕

        if (!ce.isIntact()) {
            JSONObject result = new JSONObject();
            result.put("Msg", "Insufficient parameters.");
            result.put("ErrorCode", 2);
            return result;
        }

        ts.setFuncName("insert_certificate");
        ts.setFuncParam(ce.submit());
        Pair<Integer, String> obj = ts.sendTransaction();

        JSONObject result = new JSONObject();
        if (obj.getFirst() == 0) {
            System.out.println("insert_certificate results:" + obj);
            result.put("Msg", "SUCCESS");
            result.put("ErrorCode", 0);
            if (obj.getSecond().equals("null")) {
                result.put("Data", obj.getSecond());
            } else {
                result.put("Data", JSON.parseObject(obj.getSecond()));
            }
        } else {
            result.put("Msg", "ERROR in sendTransaction.");
            result.put("ErrorCode", 1);
        }

        return result;
    }

    @RequestMapping("/test")
    public String test(@RequestBody JSONObject jsonParam) {
        JSONObject data = jsonParam.getJSONObject("data");
//        System.out.println(data);

        System.out.println(data.getString("company_address"));
        if (data.getString("company_address") == null) {
            System.out.println(123456);
        }
        return data.getString("company_address");
    }

    @ResponseBody
    @RequestMapping(value = "/json", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public JSONObject getByJSON(@RequestBody JSONObject jsonParam) {
        // 直接将json信息打印出来
        System.out.println(jsonParam.toJSONString());

        // 将获取的json数据封装一层，然后在给返回
        JSONObject result = new JSONObject();
        result.put("msg", "ok");
        result.put("method", "json");
        result.put("data", jsonParam);

        return result;
    }

    @RequestMapping("/test1")
    public String test1() {
        CertificateEntity ce = new CertificateEntity();
        ce.setCompany_id("1");
        ce.setCertificate_number("2");
        ce.setDay("15");
        return ce.submit();
    }

    @RequestMapping("/handle")
    public String handle() {
        TransactionService ts = new TransactionService();
        Object obj = ts.sendTransaction();
        String objStr = "ERROR";
        if (Objects.nonNull(obj)) {
            objStr = JSON.toJSONString(obj);
        }
        return objStr;
    }

    @RequestMapping("/paillier")
    public String crypt() {
        // generate the key pair for encrypt and decrypt
        KeyPair keypair = PaillierKeyPair.generateGoodKeyPair();
        RSAPublicKey pubKey = (RSAPublicKey) keypair.getPublic();
        RSAPrivateKey priKey = (RSAPrivateKey) keypair.getPrivate();

        // encrypt the first number with public key
        BigInteger i1 = BigInteger.valueOf(1000000);
        String c1 = PaillierCipher.encrypt(i1, pubKey);

        // encrypt the second number with same public key
        BigInteger i2 = BigInteger.valueOf(2012012012);
        String c2 = PaillierCipher.encrypt(i2, pubKey);

        // paillier add with numbers
        String c3 = PaillierCipher.ciphertextAdd(c1, c2);

        /* decrypt the result */
        BigInteger o3 = PaillierCipher.decrypt(c3, priKey);

        return "C1:" + c1 + ",C2:" + c2 + ",C3:" + c3;
    }

    public static void paillier_test() {
        KeyPair keypair = PaillierKeyPair.generateGoodKeyPair();
        RSAPublicKey pubKey = (RSAPublicKey) keypair.getPublic();
        RSAPrivateKey priKey = (RSAPrivateKey) keypair.getPrivate();

        System.out.println("RSAPublicKey");
        String pubstr = PaillierKeyPair.publicKeyToPem(pubKey);
        System.out.println(pubstr);

        System.out.println(pubKey.toString());
        String pub = Base64.encodeBase64String(pubKey.getEncoded());
        System.out.println(pub);
        String pri = Base64.encodeBase64String(priKey.getEncoded());
        System.out.println(pri);

        System.out.println("RSAPrivateKey");
        String pristr = PaillierKeyPair.privateKeyToPem(priKey);
        System.out.println(pristr);

        String cp_pub= "-----BEGIN PUBLIC KEY-----\n" +
                "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAuNbhJjGqD9cIlwNUAqos\n" +
                "Eh32yJD0lwgoTFbYIzk0QuDvt2MD2mOz1nOUPY72IV80/00Rdn/nIMIiHRvUyptn\n" +
                "cQIrwsGnhAlE02JNg3LkpAxzWbCZ8RAKFKMGtqFfS2XYEGZs1TLjR+2NSpUMDD9i\n" +
                "gWlpsa49ADDsA8WrjD6aqEba79RP4XvNypbdW47twf9kAiDtKFjJaioBWRplIWUC\n" +
                "qRQ9F6N+awnPxqgUpTxeNhEI12qRzryjZ4sAh3VPmRa3rTTCqa0UXbUVZVniiVPX\n" +
                "qDl/XPOLJJRQfjHwY1s/5f9Lq//OLWy8cd2naaWTF88rKY6PoAieZZH+fRxUbb47\n" +
                "WQIDAQAB\n" +
                "-----END PUBLIC KEY-----\n";

        String cp_pri = "-----BEGIN PRIVATE KEY-----\n" +
                "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQC41uEmMaoP1wiX\n" +
                "A1QCqiwSHfbIkPSXCChMVtgjOTRC4O+3YwPaY7PWc5Q9jvYhXzT/TRF2f+cgwiId\n" +
                "G9TKm2dxAivCwaeECUTTYk2DcuSkDHNZsJnxEAoUowa2oV9LZdgQZmzVMuNH7Y1K\n" +
                "lQwMP2KBaWmxrj0AMOwDxauMPpqoRtrv1E/he83Klt1bju3B/2QCIO0oWMlqKgFZ\n" +
                "GmUhZQKpFD0Xo35rCc/GqBSlPF42EQjXapHOvKNniwCHdU+ZFretNMKprRRdtRVl\n" +
                "WeKJU9eoOX9c84sklFB+MfBjWz/l/0ur/84tbLxx3adppZMXzyspjo+gCJ5lkf59\n" +
                "HFRtvjtZAgMBAAECggEAVy+8Wei5P+l9OsYuHWepjA4oNujxBkCpyYB5SPwVo6o4\n" +
                "SHdi/ONkk7ZG6VaC0LD47aerogqoufH4Z2SWVc9B1rNgcugnnQdGOLM4vtU19g+N\n" +
                "ZXQA2Q1ow902MHUj3wu2A95cXaA4RthNei2d1jUbwNE5Mr9uNKXe3dSim294QgED\n" +
                "/MPo9n+21SXqExviAV5WegGFcOrFtlDmPaKimgP0y0VhpN3MkV+yxxH8hxc66zuz\n" +
                "H6hWHtX+96bDJ9relTYONRa6lFR3tAUpcacC4hDhwjm+4xiacUVaz8bH/JqGgECp\n" +
                "CyzsAqAzaa6m9jMYsqrQwIVH3ohLHFbn9Ql3Q/d1EQKBgQDuGzaZmXFYaVhzlsxe\n" +
                "WEdA6BBEQrBqbsBBNZKzRz/R/kXKlL/2wbqOdhwoP90MUzPh5RYULzORjfO2t9j/\n" +
                "urzoj3+VsSakCEzznAtm3RMyIv/9hEYqMYz275kn+4uiWxUaW886G4zghC7Py45R\n" +
                "FxcGXAvBy27la8YBWKZ2qSZdxQKBgQDGuuUqVyxovEYlY8DNuksl7ELdmH+++m5K\n" +
                "2KKMjleEZcngcY1ml7nmiXDr4cSRKOkKh83Fs8YjQH0JxloVL8JQzvHbBL1cMp48\n" +
                "+bUAUFtY0WzPGIFopuib46Hzw2PEYCHba8APCuy4OoytMZJ3yrZxT5wofMrd5Zk9\n" +
                "EPiTbm20hQKBgCSIClWApFJDP23kmhBzjOMZd71xWe3KdjbLCGn3KYSHdZ/COL2c\n" +
                "NYo79s/6FGkaruAerMeGAmx3kgfbpzB9LxSjR5jo6BvMpjnOHDW4/4P2uL3luHoq\n" +
                "xgzw79ehkWfkf5Vetqmcpn2f6iq5eWlZmI/COY+PLcGHSobUWh3kj91dAoGBALu7\n" +
                "Vj0XOJaANOuhPkOaFt9uPwRCyXrJ7GK8H2IUHUyvM3PZETC3aCAF+ix6jExsjykp\n" +
                "fROkbipjAzu2quaUxUW9GPBDm/Z6wszrPzdasdBHljhU9/q6ocaR1S4HiDm8bn9v\n" +
                "XHFZaqHmfXom+Cw9zPdhsyz0JlhUStxLKedsTCpZAoGBAOzyxJFrOyToMxBecA+G\n" +
                "FGdC6ZTURdlJV1VILsff8+yEMO+Zj6AJ1FS19Z/DTBJ3DuCm1R4icis6gj4wuN1/\n" +
                "0/QnPrw4mPQyRbWdUUNFcLYEpEgriQmlOegmNb3fTi32cgCy4Ry4GTEFseXuAntI\n" +
                "rSD9gLvPVbg7cPNP0zEdeuHa\n" +
                "-----END PRIVATE KEY-----\n";

        RSAPublicKey cp_pubKey = (RSAPublicKey) PaillierKeyPair.pemToPublicKey(cp_pub);
        RSAPrivateKey cp_priKey = (RSAPrivateKey) PaillierKeyPair.pemToPrivateKey(cp_pri);

        BigInteger i1 = BigInteger.valueOf(1000000);
        String c1 = PaillierCipher.encrypt(i1, cp_pubKey);

        BigInteger i2 = BigInteger.valueOf(2012012012);
        String c2 = PaillierCipher.encrypt(i2, cp_pubKey);


        try {
            FileWriter fw = new FileWriter("pubKey.pem");
            fw.write("");//清空原文件内容
            fw.write(pubstr);
            fw.flush();
            fw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            FileWriter fw = new FileWriter("priKey.key");
            fw.write("");//清空原文件内容
            fw.write(pristr);
            fw.flush();
            fw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        System.out.println(PaillierCipher.decrypt(c1,cp_priKey));
        System.out.println(PaillierCipher.decrypt(c2,cp_priKey));
    }

    public static void main(String[] args) {

//        BigInteger n = ((RSAPublicKey)pubKey).getModulus();
//        BigInteger random;
//        do {
//            random = new BigInteger(n.bitLength(), new Random());
//        } while (random.signum() != 1);

        BigInteger random = new BigInteger("30261505444840429861664043025274334793504281263492215551905535852297839096170084927144294733259086527255215019853372269670416843964403568455238862521808111563955694182363967509211732992048551686391577618308124318586613114510178187486610915016565417976620246966286438250808818694367445118114073360151324288915881906465234354440364924297864789229323577331101907514864175612163026772832066443872015131241212872407733039221453528950939939073023052479938438623202757768000570848230003962604804522706152758760241565531720616959392155221548266226754548606228001342483604007595730833812366150830395003551507799648689344999616");

        String str = "-500";
        BigInteger bin = BigInteger.valueOf(Long.parseLong(str));
        String encrypt_str = PaillierCipher.encrypt(bin, pubKey);
        System.out.println(PaillierCipher.decrypt(encrypt_str,priKey));

        BigInteger i1 = BigInteger.valueOf(5);
        String c1 = MyPaillierCipher.encrypt(i1, pubKey, random);
        System.out.println("c1:"+c1);

        BigInteger i2 = BigInteger.valueOf(15);
        String c2 = MyPaillierCipher.encrypt(i2, pubKey, random);
        System.out.println("c2:"+c2);

        BigInteger i3 = BigInteger.valueOf(11);
        String c3 = MyPaillierCipher.encrypt(i3, pubKey, random);
        System.out.println("c3:"+c3);

        BigInteger i4 = BigInteger.valueOf(9);
        String c4 = MyPaillierCipher.encrypt(i4, pubKey, random);
        System.out.println("c4:"+c4);

        BigInteger i5 = BigInteger.valueOf(10);
        String c5 = MyPaillierCipher.encrypt(i5, pubKey, random);
        System.out.println("c5:"+c5);

        BigInteger i6 = BigInteger.valueOf(20);
        String c6 = MyPaillierCipher.encrypt(i6, pubKey, random);
        System.out.println("c6:"+c6);

        String s1 = "0100A51A7ADA908C8D0ACAFFA64833407E11B8E303C2B9B25AFB691E3FF3F15339F5E718DE474FF840EB09A39803B22F84A3340E64C9802C1C644F22023C09CC45984FEC4D157E4477C6581813B2E63F3B72CACE9032A69E6C553C75F18D3A56FBE43DC5E580A1755CD38367AB5B03B5A5CB337B69ABF981B0545C0BE112A99320DD174E53819C8FBFDF7B2F73468879FC0EEA1773E103F873258915EC430ABD6F03E891558FD562790D032176B5A23BBA8CF49D51846D0D2F0B154490F04751074BA92AE44FDE3D3196C755A064312C5B175C24D8807A0C062C58A7FA816C479728405C1B87B2C56FD7627FAFBDCC20FFDBFD9D195C1D9C12F242F77D9FD8F4F1193867B44D8649CBA41598508A15F01CB37643123BA02AD793CFF6E538DB9999AB039624AEC104E2359A63C04E0A337CA866641C7FC3C50FFA771C6D450E9AD0F3E698AC3F535F82A3E004F9901CB19FAE033B6996176047F18FE3328162BB5FDA56D96F6D3A2B7EF8FC530DEF8D67E819F152300BCDE9643668E639F6B15E0F29B1F83FD74F5F8CC02D9F4DECAC417CC6D0E9DE0FA590E731F6E2FF5F8108415E1FC3C6E6728588B64EEAE13D0FE948021F85CE3DF1D84E904DEE2E29643FBFA7EB54CEDFA9FEE958767473CAAB32588A76836F4A092CFE4AE7661D2C4A7619A514A734235329D417C80A29DA66B5B505E4BB16AD8D530C235A32A981CB9205C26363851761624D0A578D71DDA0B836C897E1D389653119BF0E69313491236430C6704625C86EA5F91D64D7FBD68827FA82530CA142C7FFA533555EC57286769B4C40A86CE59FD820130635CB6B6A2B24F563384ABDBD01B4B46CB3937B5FA69CA80CB73231392C78C02F3208969BAF1E68E4FFA3186F06C5F25CCA287358208B700B1371B0821A7879A1A0D9427C9018DE66398A2DAB8B871A2F23938CF68EC6FE7B7254AF311EB55129D4D897D9733EF11CBCBE4EB3A42D8F4CCA32E1F80EB729DA93DFEA66B9301EB8087AB9DF21AEA2C0BDAEDB01C21E8C1D0B316B3BE1B09A1FC14E748480F7F082B1088367202ABF0F79EAB5000A035C7D6BD93BFCE7CA";
        String s2 = "0100A51A7ADA908C8D0ACAFFA64833407E11B8E303C2B9B25AFB691E3FF3F15339F5E718DE474FF840EB09A39803B22F84A3340E64C9802C1C644F22023C09CC45984FEC4D157E4477C6581813B2E63F3B72CACE9032A69E6C553C75F18D3A56FBE43DC5E580A1755CD38367AB5B03B5A5CB337B69ABF981B0545C0BE112A99320DD174E53819C8FBFDF7B2F73468879FC0EEA1773E103F873258915EC430ABD6F03E891558FD562790D032176B5A23BBA8CF49D51846D0D2F0B154490F04751074BA92AE44FDE3D3196C755A064312C5B175C24D8807A0C062C58A7FA816C479728405C1B87B2C56FD7627FAFBDCC20FFDBFD9D195C1D9C12F242F77D9FD8F4F1192464B09FDE8DD71AB0BF50583ACBBA18D4A274FE1950EA43CD396323E4CE4F080750AD710C97E3A9A9B72FC5273ACABABEE797E6BA06BED7FE825E4CA02A93AD5EC802A65A8FBB4FD1A9805CFC2B0F361CC565CE797789F1AC1280D4F02B4C4FE3080F5C4CE4FBEEBA81E44A2CA2435CABC6A0500BB00C6D703CF990012B4D5ADC2BD193AF71EE012540B5CE0D95477CD5A272C285029ACECD37D19E0A3C004A0191C593967494A8C0C47CFA6A4C61EA4BA72412D855798FAE18ADDD998772E5CAAA8D554F6B520FCD1F7652D15A2E259FB7BC5C6A3C30B63179706F399A62268FD7F6D7B69096337CA5F1D898A1BDF274FE68E0D6C298E3FD80ADCDB21B0D3D505B20176306379CB420653BCFDDFFA16D759327D3E955BD3D3CFE1418D20EED92DCF3D6316C378E16B3A127A52F1CDE5FE6C62F98A2F69FC142E638AC988346DEA556873F0F47CB7C3F8712F94E51E137CA793418459AC64BE5AC16246480AED2D1D7E0BAA20322564C0C249D611889382F063244FD6D1F4380CEDBD9ABA2516D6F6FAA93CD4B682825E874AE599125B99802CFD94585568F36326F6D831A7EDE3ADA78AF85782F128670F5153753BF46A5A8872EB55ECCA60B60DC1355944F7DA91F6E27E73AFF5191989E86F4A0C7569D4008A4BDE0320EF6E6FBF80929E1CE37F1F0C2BDE589248076BDC48F03170B1834C3DF00BE812F2681F089D84548";
        System.out.println(PaillierCipher.decrypt(s1,priKey));
        System.out.println(PaillierCipher.decrypt(s2,priKey));
    }
}
